package com.tofine.smarthome.rest;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tofine.smarthome.Constants;
import com.tofine.smarthome.common.gson.CategoryTypeAdapter;
import com.tofine.smarthome.common.gson.DeviceTypeAdapter;
import com.tofine.smarthome.common.gson.RoomTypeAdapter;
import com.tofine.smarthome.model.Category;
import com.tofine.smarthome.model.Device;
import com.tofine.smarthome.model.Room;
import com.tofine.smarthome.rest.service.ApiService;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author ddMax
 * @since 2016/5/28 23:18
 * 说明：REST请求客户端
 */
public class RestClient {

    private ApiService apiService;

    private static RestClient client;

    private RestClient() {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Device.class, new DeviceTypeAdapter())
                .registerTypeAdapter(Category.class, new CategoryTypeAdapter())
                .registerTypeAdapter(Room.class, new RoomTypeAdapter())
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        apiService = retrofit.create(ApiService.class);
    }

    public static RestClient getInstance() {
        if (client == null) {
            client = new RestClient();
        }
        return client;
    }

    public ApiService getApiService() {
        return this.apiService;
    }
}
